<template>
	<view class="app">
		<view class="app-container">
			<view class="action">
				<button class="cu-btn block bg-primary margin-tb-sm lg" @tap="chooseMessageFile">微信导入</button>
				<view class="text-center">仅支持上传{{ format }}格式文件</view>
				<!-- <view class="text-center text-gray">
					<text class="cuIcon-question"></text>
					使用帮助
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
import { VUE_APP_BASE_API } from '@/common/tools/config.js';
import { mapMutations, mapGetters } from 'vuex';
import {LIMIT_SIZE} from "../common/tools/config";
export default {
	data() {
		return {
			doc: ['pdf', 'doc', 'docx'], // ['pdf', 'doc', 'docx', 'xls', 'xlsx']
			img: ['png', 'jpg']
		};
	},
	computed: {
		...mapGetters(['token']),
		selectedSpec() {
			return getApp().globalData.uploadFileInfo['spec']
		},
		format() {
			const list = this.selectedSpec.type === 0 ? this.doc : this.img;
			return list.join('/');
		}
	},
	methods: {
		chooseMessageFile() {
			if (this.selectedSpec.type === 0) {
				uni.chooseMessageFile({
					count: 1,
					type: 'file',
					extension: this.selectedSpec.type === 0 ? this.doc : this.img,
					success: res => {
						const file = res.tempFiles[0];
						if(file.size / 1024 /1024 <= LIMIT_SIZE){
							uni.showLoading({ title: '上传中', mask: true });
							uni.uploadFile({
								url: VUE_APP_BASE_API + '/applet/Upload/uploadWechatFile',
								header: {
									'X-Token': this.token,
									'content-type': 'multipart/form-data'
								},
								formData: {
									upload_type: 1,
									file_name: file.name
								},
								filePath: file.path,
								name: 'file',
								success: e => {
									uni.hideLoading();
									const data = JSON.parse(e.data);
									console.log(e);
									if(data.code === 200) {
										//存储文件
										getApp().globalData.uploadFileInfo['path'] = data.result.upload_file;
										getApp().globalData.uploadFileInfo['file_name'] = data.result.file_name;
										getApp().globalData.uploadFileInfo['page_num'] = data.result.page_num;
										//去确认订单
										uni.redirectTo({
											url: '/print-pages/confirm'
										});
									}else {
										console.error(e);
										uni.showToast({
											title: '服务器繁忙，请稍候再试',
											icon: 'none',
											duration: 5000
										});
									}
								},
								fail: e => {
									console.error(e);
									uni.showToast({
										title: '服务器繁忙，请稍候再试',
										icon: 'none',
										duration: 5000
									});
								}
							});
						}else  {
							uni.showToast({
								title: `请上传小于等于${LIMIT_SIZE}M的文件`,
								icon: 'none',
								duration: 5000
							});
						}
					}
				});
			} else {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'], //从相册选择
					success: res => {
						//获取第一个
						const file = res.tempFilePaths.shift();
						//存储文件
						getApp().globalData.uploadFileInfo['path'] = file;
						getApp().globalData.uploadFileInfo['page_num'] = 1;
						//去裁剪
						uni.navigateTo({
							url: '/print-pages/image-cropper'
						});
					}
				});
			}
		}
	}
};
</script>

<style lang="scss">
.app-container {
	height: 100%;
	position: relative;
	.upload-web {
		padding: 0 20px;
		font-size: 16px;
		height: 40px;
	}
	.upload {
		// display: none;
	}
	.action {
		bottom: 36px;
		left: 16px;
		right: 16px;
		position: absolute;
		height: 35%;
	}
}
</style>
